<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Model;

use Common\Model\ModelModel;

/**
 * 文章模型
 * @author jry <598821125@qq.com>
 */
class InviteorderModel extends ModelModel
{
    /**
     * 数据库真实表名
     * 一般为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如微信模块用weixin作为数据表前缀
     * @author jry <598821125@qq.com>
     */
    protected $tableName = 'admin_inviteorder';

    /**
     * 自动验证规则
     * @author jry <598821125@qq.com>
     */
    protected $_validate = array(
        array('name', 'require', '姓名不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('mobile', 'require', '手机号不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('jointime', 'require', '参与场次不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('joinnum', 'require', '购买数量不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //array('title', '', '标题已经存在', self::VALUE_VALIDATE, 'unique', self::MODEL_BOTH),
    );

    /**
     * 自动完成规则
     * @author jry <598821125@qq.com>
     */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
        array('status', '1', self::MODEL_INSERT),
    );

    protected function _after_find(&$result, $options) {
        /*
        $post_object   = D('Admin/Invitation');
        $condition = array('id'=>$result['inviteid']);
        $post_info = $post_object->field('title')->where($condition)->find();
        $result['postname'] = $post_info['title'];
        */
    }

    

    /**
     * 查找后置操作
     * @author jry <598821125@qq.com>
     */
    protected function _after_select(&$result, $options)
    {
        foreach ($result as &$record) {
            $this->_after_find($record, $options);
        }
    }

    public function getCount($id)
    {
        $con['status'] = array("eq",1);
        $con['inviteid'] = intval($id);
        $con['ispay'] = 1;
        $data = $this->field('jointime,sum(joinnum) as cnt')
            ->group('jointime')
            ->where($con)
            ->select();
        foreach ($data as $key=>$val) {
            $list[$val['jointime']] = $val['cnt'];
        }
        return $list;
    }

    public function oneCount($id,$time)
    {
        $con['status'] = array("eq",1);
        $con['inviteid'] = intval($id);
        $con['jointime'] = $time;
        $con['ispay'] = 1;
        $data = $this->field('sum(joinnum) as cnt')
            ->where($con)
            ->select();
        return $data[0]['cnt'];
    }

    /**
     * 获取列表
     * @author jry <598821125@qq.com>
     */
    public function getList($cid, $limit = 10, $page = 1, $order = null, $map = null)
    {
        $con["status"] = array("eq", '1');
        if ($map) {
            $map = array_merge($con, $map);
        }
        if (!$order) {
            $order = 'id desc';
        }
        $list = $this->page($page, $limit)
            ->order($order)
            ->where($map)
            ->select();


        return $list;
    }
}
